<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-01-31 10:53:31
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-01-31 15:45:30
 * @Description:
 *
-->

<template>
  <el-dialog
    class="associate-dialog"
    title="绑定浮动IP"
    :visible="show"
    width="35vw"
    top="15vh"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="closeClick"
  >
    <el-form
      ref="form"
      class="associate-form"
      :rules="rules"
      :model="form"
      label-width="auto"
      size="medium"
    >
      <el-form-item label="IP地址" prop="ip">
        <el-input v-model="form.ip" placeholder="请输入" :disabled="true" />
      </el-form-item>
      <el-form-item label="云服务商" prop="cloudPlatform">
        <el-input
          v-model="form.cloudPlatform"
          placeholder="请输入云服务商"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="云账号" prop="cloudAccountName">
        <el-input
          v-model="form.cloudAccountName"
          placeholder="请输入云账号"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="待连接的端口" prop="portId">
        <el-select
          v-model="form.portId"
          placeholder="请选择待连接的端口"
          style="width: 100%"
          :loading="loadingAssociatePort"
        >
          <el-option
            v-for="item in portOptions"
            :key="item.portId"
            :label="item.vmName + ': ' + item.portId"
            :value="item.portId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancelClick">取 消</el-button>
      <el-button
        type="primary"
        size="small"
        :loading="confirmLoading"
        @click="confirmClick"
      >确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { associateFloatIP, getFloatIPPorts } from '@/api/cn-resource'

export default {
  name: 'AssociateDialog',
  components: {},
  mixins: [],
  props: {
    // 是否显示
    show: {
      type: Boolean,
      default: false
    },
    info: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      portOptions: [], // 待连接的端口列表
      form: {
        id: '',
        ip: '',
        cloudPlatform: '',
        cloudAccountName: '',
        portId: ''
      },
      rules: {
        portId: [
          { required: true, message: '请选择待连接的端口', trigger: 'change' }
        ]
      },
      loadingAssociatePort: false,
      confirmLoading: false
    }
  },
  computed: {},
  watch: {
    info: {
      handler(newVal) {
        if (newVal.id) {
          this.form = newVal
          this.getFloatIPPorts()
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {},
  methods: {
    /**
     * @description: 获取待连接的端口
     * @return {*}
     */
    getFloatIPPorts() {
      this.loadingAssociatePort = true
      getFloatIPPorts({
        cloudAccountName: this.form.cloudAccountName
      })
        .then((res) => {
          this.portOptions = Array.isArray(res.data) ? res.data : []
        })
        .finally(() => {
          this.loadingAssociatePort = false
        })
    },
    /**
     * @description: 确定
     * @return {*}
     */
    confirmClick() {
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          this.confirmLoading = true
          associateFloatIP({
            id: this.form.id,
            portId: this.form.portId
          })
            .then((res) => {
              this.$emit('success')
              this.closeClick()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 关闭
     * @return {*}
     */
    closeClick() {
      this.$emit('update:show', false)
    },
    /**
     * @description: 取消
     * @return {*}
     */
    cancelClick() {
      this.closeClick()
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.associate-dialog {
  /deep/ .el-dialog {
    min-width: 600px;
  }
}
</style>
